package Fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.hjh.qinxin.R;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import adpter.GlideImageLoader;
import adpter.ReportListAdapter;
import model.ReportListModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import web.BaseApplication;
import web.HotReport.HorReportRespon;
import web.HotReport.ValuesHotReport;
import web.MediaReport.MeReResponse;
import web.MediaReport.ValuesMeRe;
import web.Report.ReportRespone;
import web.Report.ValuesReport;


public class MediaFragment extends Fragment{
    private View root;
    private Banner banner;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        root = inflater.inflate(R.layout.fragment_media, container, false);
        init();
        initView();
        ReportList(root);
        return root;
    }
    private void init(){
        banner = root.findViewById(R.id.banner);
    }
    private void initView(){
        List images = new ArrayList();
        Call<ReportRespone<ValuesReport>> picture = BaseApplication.httpbinService.Picture();
        picture.enqueue(new Callback<ReportRespone<ValuesReport>>() {
            @Override
            public void onResponse(Call<ReportRespone<ValuesReport>> call, Response<ReportRespone<ValuesReport>> response) {
                for (int i = 0; i < response.body().getValues().getHot_baodao().size(); i++) {
                    images.add(response.body().getValues().getHot_baodao().get(i).getUrl());
                }
                    banner.setImageLoader(new GlideImageLoader());
                    banner.setImages(images);
                    banner.start();
            }

            @Override
            public void onFailure(Call<ReportRespone<ValuesReport>> call, Throwable t) {

            }
        });
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Toast.makeText(getActivity(), "position" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }
    private void ReportList(View root){
        List<ReportListModel> data  =new ArrayList<>();
        RecyclerView recyclerView = root.findViewById(R.id.report_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        ReportListAdapter adapter = new ReportListAdapter(getActivity());
        adapter.setData(data);
        recyclerView.setAdapter(adapter);
        Call<MeReResponse<ValuesMeRe>> call = BaseApplication.httpbinService.MediaReport();
        call.enqueue(new Callback<MeReResponse<ValuesMeRe>>() {
            @Override
            public void onResponse(Call<MeReResponse<ValuesMeRe>> call, retrofit2.Response<MeReResponse<ValuesMeRe>> response) {
                for (int i = 0;i < response.body().getValues().getMtbd_ar_list().size();i++){
                    ReportListModel item = new ReportListModel();
                    item.setUrl(response.body().getValues().getMtbd_ar_list().get(i).getUrl());
                    item.setListHeadline(response.body().getValues().getMtbd_ar_list().get(i).getTitle());
                    item.setListTime(response.body().getValues().getMtbd_ar_list().get(i).getAdd_time());
                    data.add(item);
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<MeReResponse<ValuesMeRe>> call, Throwable t) {

            }
        });

    }



}